﻿namespace Vampirewal.Core.WPF.Theme;

/// <summary>
/// PopupWindow.xaml 的交互逻辑
/// </summary>
[VampirewalIoCRegister("PopupWindow", RegisterType.Transient)]
public partial class PopupWindow : WindowBase, IPopupWindow
{
    /// <summary>
    /// 在窗体内弹出提示框
    /// </summary>
    public PopupWindow()
    {
        InitializeComponent();
        this.DataContext = this;
    }

    public void Setting(PopupWindowSetting setting)
    {
        this.Owner = (Window)setting.OwnerWindow;
        isKeepOpen = setting.IsKeepOpen;
        switch (setting.Type)
        {
            case MessageType.Information:
                bg.Background = Brushes.YellowGreen;
                break;

            case MessageType.Error:
                bg.Background = Brushes.Red;
                break;

            case MessageType.Successful:
                bg.Background = Brushes.Green;
                break;

            default:
                break;
        }
        MessageText.Text = setting.Message;

        this.Loaded += PopupWindow_Loaded;
    }

    private void PopupWindow_Loaded(object sender, RoutedEventArgs e)
    {
        if (isKeepOpen == false)
        {
            DoubleAnimation animation1 = new DoubleAnimation()
            {
                To = this.Owner.Top,
                BeginTime = TimeSpan.FromSeconds(2),
                Duration = TimeSpan.FromSeconds(1),
            };
            animation1.Completed += Animation1_Completed;
            this.BeginAnimation(Window.TopProperty, animation1);

            DoubleAnimation animation_Op = new DoubleAnimation
            {
                To = 0,
                BeginTime = TimeSpan.FromSeconds(2),
                Duration = TimeSpan.FromSeconds(1)
            };
            this.BeginAnimation(Window.OpacityProperty, animation_Op);
        }
    }

    private void Animation1_Completed(object sender, EventArgs e)
    {
        this.Close();
    }

    public bool isKeepOpen { get; set; }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        this.Close();
    }
}